/**
 * 获取uuid
 * @returns {string} 生成一个随机的uuid
 */
export function getUUID() {
  return Math.random().toString(36).substring(2) + Date.now().toString(36);
}

/**
 * rpx 换算为 px
 * @param upx
 */
export const rpx2px = (upx) => {
  return uni.upx2px(upx);
};

/**
 * px 换算为 rpx
 * @param upx
 */
export const px2rpx = (px) => {
  return px / (uni.upx2px(100) / 100);
};

/**
 * 获取微信右上角胶囊信息
 * @returns {object} 返回胶囊信息
 */
export const getWxMenuButton = () => {
  let menuInfo = uni.getMenuButtonBoundingClientRect();
  return menuInfo;
};

/**
 * 获取navbar高度
 * @returns {Promise<NavInfo.height>} 整个navbar的高度
 * @returns {Promise<NavInfo.contentHeight>} navbar内容区域的高度
 * @returns {Promise<NavInfo.statusBarHeight>} 状态栏高度
 * @returns {Promise<NavInfo.menuButtonInfo>} 胶囊信息
 */
export const getNavBarHeight = () => {
  const menuButtonInfo = getWxMenuButton();
  return new Promise((resolve) => {
    uni.getSystemInfo({
      success: (res) => {
        let navHeight =
          menuButtonInfo.height +
          (menuButtonInfo.top - res.statusBarHeight) * 2 +
          res.statusBarHeight;
        resolve({
          height: navHeight,
          contentHeight: navHeight - res.statusBarHeight,
          statusBarHeight: res.statusBarHeight,
          menuButtonInfo: menuButtonInfo,
        });
      },
    });
  });
};

/**
 * 获取wxml节点并返回节点信息,异步返回boundingClientRect结果  最好使用async await同步
 * @param name { String } css选择器的标签名称
 */
export const selectEl = (name) => {
  const query = wx.createSelectorQuery();
  return new Promise((resolve, reject) => {
    query
      .select(name)
      .boundingClientRect((res) => {
        resolve(res);
      })
      .exec();
  });
};
